package com.yksj.healthtalk.ui.doctor;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.adapter.DoctorServiceAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.ServiceContent;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.JsonParseUtils;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 医生馆服务内容 
 * @author Administrator
 *
 */
public class DoctorServiceActivity extends BaseFragmentActivity implements OnClickListener{
	
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private ArrayList<ServiceContent> contents;
	private DoctorServiceAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.doctor_service);
		initWidget();
		initData();
	}

	private void initData() {
		contents = new ArrayList<ServiceContent>();
		RequestParams params = new RequestParams();
		
		if(getIntent().hasExtra("ORIDERID")){
			params.put("ORDER_ID", getIntent().getStringExtra("ORIDERID"));
			params.put("Type","queryCustomerOrderServiceContent" );
		}else if(getIntent().hasExtra("service_type")){
				params.put("Type","queryDoctorServerByServiceType" );
				params.put("SERVICE_ITEM_ID", getIntent().getExtras().getString("service_item_id"));
				params.put("SERVICE_TYPE_ID", getIntent().getExtras().getString("service_type"));
				params.put("CUSTOMER_ID", getIntent().getExtras().getString("userId"));
		}else {
			params.put("CUSTOMER_ID", getIntent().getExtras().getString("userId"));
			params.put("Type","queryDoctorServer" );
		}
		
		adapter = new DoctorServiceAdapter(this,contents);
		mListView.setAdapter(adapter); 
		
		mPullToRefreshListView.setRefreshing();
		
		
		HttpRestClient.doHttpSearchServiceContent(params, new  JsonsfHttpResponseHandler(){
			
			@Override
			public void onFinish() {
				super.onFinish();
				mPullToRefreshListView.onRefreshComplete();
			}

			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				List<ServiceContent> content = JSON.parseArray(response.toString(),ServiceContent.class);
				if (content == null || content.size() == 0) {
					ToastUtil.showShort(getApplicationContext(), "没有服务内容");
				}else {
					contents.addAll(content);
					adapter.notifyDataSetChanged();
				}
				super.onSuccess(statusCode, response);
			}
			
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				String message = JsonParseUtils.filterErrorMessage(response.toString());
				if (message != null) {
					ToastUtil.showShort(getApplicationContext(), message);
				}
				super.onSuccess(statusCode, response);
			}
		});
	}

	private void initWidget() {
		initTitle();
		if(!getIntent().hasExtra("NN")){
			titleTextV.setText("服务内容");
		}else {
			titleTextV.setText("服务介绍");
		}
		if(getIntent().hasExtra("isbuy")){
			titleTextV.setText("所享受服务");
		 }
		 titleLeftBtn.setOnClickListener(this);
		 
		 
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.hall);
		mListView = mPullToRefreshListView.getRefreshableView();
		
		mListView.setFooterDividersEnabled(false);
		mListView.addFooterView(getLayoutInflater().inflate(R.layout.line_layout, null));
		mListView.setEmptyView(getLayoutInflater().inflate(R.layout.listview_empty_view, null));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}

}
